package pl.ptemich.bumbastic;

import org.newdawn.slick.Graphics;
import org.newdawn.slick.Image;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class Sprite {

	private static final Logger LOGGER = LoggerFactory.getLogger(Sprite.class); 
	
	
	
    private static final int HORIZONTAL_SPEED_UNIT = 100;
    
    private static final int VERTICAL_SPEED_UNIT = 400;
    
    private static final int MAX_JUMP_HEIGHT = 100;
    
    private enum State {
        walk, jump;
    };    
    
    private Image image;
    
    private float posX;
    
    private float posY;
    
    private float verticalLevel;
    
    private float horizontalSpeed = 0;
    
    private float verticalSpeed = 0;
    
    private State state = State.walk;
    
    public Sprite(Image image, float posX, float posY) {
        this.image = image;
        this.posX = posX;
        this.posY = posY;
        this.verticalLevel = posY;
    }
    
    public void update(long timePassed) {
    	float move = (timePassed / 1000f) * horizontalSpeed;
        posX += move;
        LOGGER.debug(String.valueOf(move) + " " + String.valueOf(posX));
        posY += (timePassed / 1000.0) * verticalSpeed;
        
        if (State.jump.equals(state)) {
            if (posY <= verticalLevel - MAX_JUMP_HEIGHT) {
                verticalSpeed *= -1;
            }
            if (posY > verticalLevel) {
                posY = verticalLevel;
                verticalSpeed = 0;
                state = State.walk;
            }     
        }
    }
    
    public void draw(Graphics graphics) {
      image.draw(posX, posY);  
    }

    public void moveRight() {
        horizontalSpeed = HORIZONTAL_SPEED_UNIT;
    }
    
    public void moveLeft() {
        horizontalSpeed = -1 * HORIZONTAL_SPEED_UNIT;
    }    
    
    public void stop() {
        horizontalSpeed = 0;
    }
    
    public void jump() {
        if (!State.jump.equals(state)) {
            verticalSpeed = -VERTICAL_SPEED_UNIT;
            state = State.jump;
        }
    }
}
